package com.sjh.baseui.picture

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Rect
import java.io.ByteArrayOutputStream
import java.io.IOException

/**
 *    author : SunJH
 *    date   : 2021/5/18 0018
 *    desc   :
 */
object PictureUtils {
    /**
     * 将图片转换成byte数组
     */
     fun getBitmapByte(img: Bitmap): ByteArray {
        val byteArrayOutputStream = ByteArrayOutputStream()
        img.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream)
        try{
            byteArrayOutputStream.flush()
            byteArrayOutputStream.close()
        }catch (e: IOException){
            e.printStackTrace()
        }
        return byteArrayOutputStream.toByteArray()
    }

    /**
     * 叠加合并两个bitmap
     * 注意，合并两个bitmap时，前景图必须支持透明的，否则会出问题
     * @param back 背景
     * @param front 前景
     */
     fun mergeBitmap(back: Bitmap,front:Bitmap):Bitmap{
        val copy = back.copy(Bitmap.Config.ARGB_8888, true)
        val canvas = Canvas(copy)
        val bRect = Rect(0, 0, back.width, back.height)
        val fRect = Rect(0, 0, front.width, front.height)
        canvas.drawBitmap(front,fRect,bRect,null)
        return copy
    }

}